﻿using System;
using System.Data;
using System.Data.DBLib;
using SysComm;


namespace SysOperationLayer.DBHandle
{
    public class ProjectDocumentDB : DBBase
    {
        public DataTable getInfo(string projectCode, string section)
        {
            if (ConnectDB())
            {
                string strSql = "select p.*, s.TypeSeq from ProjectDocument p, DocumentType s where p.Section = s.Section and p.Type = s.Type and p.IsDelete = 0 ";

                if (projectCode != "")
                {
                    strSql += " and ProjectCode = '" + projectCode + "' ";
                }

                if (section != "")
                {
                    strSql += " and p.Section = '" + section + "' ";
                }

                return m_objDB.getDataTable(strSql, "ProjectDocument");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public DataRow getProDocInfo(string projectCode, string fileName, string section, string type)
        {
            if (ConnectDB())
            {
                string strSql = "select * from ProjectDocument where 1 = 1";

                if (projectCode != "")
                {
                    strSql += " and ProjectCode='" + projectCode + "' ";
                }

                if (fileName != "")
                {
                    strSql += " and FileName='" + fileName + "' ";
                }

                if (section != "")
                {
                    strSql += " and Section='" + section + "' ";
                }

                if (type != "")
                {
                    strSql += " and Type='" + type + "' ";
                }

                return m_objDB.getDataRow(strSql, "ProjectDocument");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public int insert(string strProjectCode, string strFileName, string strSession, string strType, string strUserID, string strOwnerGroupID, string strIP)
        {
            int ret = 0;

            if (ConnectDB())
            {
                string strSql = "INSERT INTO ProjectDocument (ProjectCode, FileName, Section, Type, CreateBy, UpdateBy, OwnerGroupID, CreateIP)";
                strSql += "VALUES (N'";
                strSql += strProjectCode;
                strSql += "', N'";
                strSql += strFileName;
                strSql += "', N'";
                strSql += strSession;
                strSql += "', N'";
                strSql += strType;
                strSql += "', N'";
                strSql += strUserID;
                strSql += "', N'";
                strSql += strUserID;
                strSql += "', N'";
                strSql += strOwnerGroupID;
                strSql += "', N'";
                strSql += strIP;
                strSql += "')";
                
                CDBError objError = new CDBError();
                m_objDB.updateData(strSql, objError);

                ret = objError.RetCode;
                m_strMessage = objError.Message;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
                ret = -1;
            }
            return ret;
        }

        public int delete(string strProjectCode, string strFileName, string strSection, string strType, string strUserID, string strIP)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update ProjectDocument set ";
                strSql += " isDelete = 1, DeleteBy = '";
                strSql += strUserID;
                strSql += "', DeleteIP = '";
                strSql += strIP;
                strSql += "', DeleteDate = getDate()";
                strSql += " where ProjectCode = '";
                strSql += strProjectCode;
                strSql += "' and FileName = '";
                strSql += strFileName;
                strSql += "' and Section = '";
                strSql += strSection;
                strSql += "' and Type = '";
                strSql += strType;
                strSql += "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }

            return -1;
        }

        public int trash(string strProjectCode, string strFileName, string strSection, string strType, string strUserID, string strIP)
        {
            if (ConnectDB())
            {
                string strSql = "delete from ProjectDocument where isDelete = 1";
                strSql += " and ProjectCode = '";
                strSql += strProjectCode;
                strSql += "' and FileName = '";
                strSql += strFileName;
                strSql += "' and Section = '";
                strSql += strSection;
                strSql += "' and Type = '";
                strSql += strType;
                strSql += "'";

                CDBError objError = new CDBError();
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }

            return -1;
        }

        public int restore(string strProjectCode, string strFileName, string strSection, string strType)
        {
            int ret = 0;
            if (ConnectDB())
            {
                string strSql = "update ProjectDocument set isDelete = 0";
                strSql += " where ProjectCode = '";
                strSql += strProjectCode;
                strSql += "' and FileName = '";
                strSql += strFileName;
                strSql += "' and Section = '";
                strSql += strSection;
                strSql += "' and Type = '";
                strSql += strType;
                strSql += "'";

                CDBError objError = new CDBError();
                m_objDB.updateData(strSql, objError);

                ret = objError.RetCode;
                m_strMessage = objError.Message;
            }
            return ret;
        }
    }
}
